/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Client;

import Shared.User;
import java.awt.Color;
import java.awt.Component;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.ListCellRenderer;

/**
 *
 * @author mosta
 */
public class StartConversation extends javax.swing.JFrame {
    DefaultListModel<User> listModel;
    ArrayList<User> friends = new ArrayList<>();
    int[] selectedFriedsIndics;
    User[] selectedFriends;
    ClientImp clientImp;
    ArrayList<String> friendsMail = new ArrayList<>();
    String identifier;
    /**
     * Creates new form StartConversation
     */
    public StartConversation(ArrayList<User> friends) {
        listModel = new DefaultListModel();
        try {
            clientImp = new ClientImp();
        } catch (RemoteException ex) {
            Logger.getLogger(StartConversation.class.getName()).log(Level.SEVERE, null, ex);
        }
        initComponents();
        this.friends=friends;
        System.out.println("liist:"+friends.toString());
        for(int i=1;i<friends.size();i++)
        {   
            if(friends.get(i).getStatus() != 4)
            {
                listModel.addElement( friends.get(i));
            }
        }
        listPane.setModel(listModel);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        centerPanel = new javax.swing.JPanel();
        scrollPane = new javax.swing.JScrollPane();
        listPane = new JList(listModel);
        westPanel = new javax.swing.JPanel();
        labelText = new javax.swing.JPanel();
        label = new javax.swing.JLabel();
        textField = new javax.swing.JTextField();
        btn = new javax.swing.JPanel();
        start = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Conversation");
        setBackground(new java.awt.Color(102, 0, 51));

        centerPanel.setLayout(new java.awt.BorderLayout());

        listPane.setModel(listModel);
        listPane.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listPaneValueChanged(evt);
            }
        });
        scrollPane.setViewportView(listPane);

        centerPanel.add(scrollPane, java.awt.BorderLayout.CENTER);

        westPanel.setBackground(new java.awt.Color(102, 0, 51));
        westPanel.setLayout(new java.awt.BorderLayout());

        labelText.setBackground(new java.awt.Color(102, 0, 51));
        labelText.setForeground(new java.awt.Color(255, 255, 255));
        labelText.setLayout(new java.awt.BorderLayout());

        label.setForeground(new java.awt.Color(255, 255, 255));
        label.setText("Conversation Name:");
        labelText.add(label, java.awt.BorderLayout.WEST);

        textField.setToolTipText("Enter here your conv. name");
        textField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textFieldActionPerformed(evt);
            }
        });
        labelText.add(textField, java.awt.BorderLayout.CENTER);

        westPanel.add(labelText, java.awt.BorderLayout.CENTER);

        btn.setBackground(new java.awt.Color(102, 0, 51));

        start.setBackground(new java.awt.Color(255, 255, 255));
        start.setForeground(new java.awt.Color(0, 0, 0));
        start.setText("Start");
        start.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startActionPerformed(evt);
            }
        });
        btn.add(start);

        westPanel.add(btn, java.awt.BorderLayout.SOUTH);

        centerPanel.add(westPanel, java.awt.BorderLayout.PAGE_END);

        getContentPane().add(centerPanel, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void textFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_textFieldActionPerformed

    private void startActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startActionPerformed

//        int selected[] = listPane.getSelectedIndices();
//        System.out.println("Selected Elements:  ");
//        for (int i = 0; i < selected.length; i++) {
//            String element =  listPane.getModel().getElementAt(selected[i]).toString();
//            System.out.println("  " + element);
//      }
   
        // TODO add your handling code here:
        selectedFriedsIndics = listPane.getSelectedIndices();
        selectedFriends = new User[selectedFriedsIndics.length+1];
        friendsMail.add(0, friends.get(0).getUserMail());
        selectedFriends[0] = friends.get(0);
        for (int i=0;i<selectedFriedsIndics.length;i++)
        {
            selectedFriends[i+1] = listModel.getElementAt(selectedFriedsIndics[i]);
            friendsMail.add(i+1, listModel.getElementAt(selectedFriedsIndics[i]).getUserMail());
        }
        Date date = new Date();
        identifier = date.toString();
        clientImp.checkIdentGroupChat(identifier, selectedFriends, friendsMail,selectedFriends[0].getUserName());
        dispose();
    }//GEN-LAST:event_startActionPerformed

    private void listPaneValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listPaneValueChanged
       
    }//GEN-LAST:event_listPaneValueChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(StartConversation.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(StartConversation.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(StartConversation.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(StartConversation.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                //new StartConversation().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel btn;
    private javax.swing.JPanel centerPanel;
    private javax.swing.JLabel label;
    private javax.swing.JPanel labelText;
    private javax.swing.JList listPane;
    private javax.swing.JScrollPane scrollPane;
    private javax.swing.JButton start;
    private javax.swing.JTextField textField;
    private javax.swing.JPanel westPanel;
    // End of variables declaration//GEN-END:variables

}
